home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / utils / adt / datum.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  5.6 KB  |  221 lines

  1. /*===================================================================
  2.  *
  3.  * FILE:
  4.  * datum.c
  5.  *
  6.  * HEADER:
  7.  * $Header: /private/postgres/src/utils/adt/RCS/datum.c,v 1.10 1991/11/09 01:47:55 mer Exp $
  8.  *
  9.  * DESCRIPTION:
  10.  *
  11.  * In the implementation of the next routines we assume the following:
  12.  *
  13.  * A) if a type is "byVal" then all the information is stored in the
  14.  * Datum itself (i.e. no pointers involved!). In this case the
  15.  * length of the type is always greater than zero and less than
  16.  * "sizeof(Datum)"
  17.  * B) if a type is not "byVal" and it has a fixed length, then
  18.  * the "Datum" always contain a pointer to a stream of bytes.
  19.  * The number of significant bytes are always equal to the length of the
  20.  * type.
  21.  * C) if a type is not "byVal" and is of variable length (i.e. it has
  22.  * length == -1) then "Datum" always points to a "struct varlena".
  23.  * This varlena structure has information about the actual length of this
  24.  * particular instance of the type and about its value.
  25.  *
  26.  */
  27.  
  28. #include "tmp/c.h"
  29. #include "tmp/postgres.h"
  30. #include "tmp/datum.h"
  31. #include "catalog/pg_type.h"
  32. #include "utils/log.h"
  33.  
  34. /* datum.c */
  35. Size datumGetSize ARGS((Datum value , ObjectId type , bool byVal , Size len ));
  36. Datum datumCopy ARGS((Datum value , ObjectId type , bool byVal , Size len ));
  37. void datumFree ARGS((Datum value , ObjectId type , bool byVal , Size len ));
  38. bool datumIsEqual ARGS((Datum value1 , Datum value2 , ObjectId type , bool byVal , Size len ));
  39.  
  40. /*-------------------------------------------------------------------------
  41.  * datumGetSize
  42.  *
  43.  * Find the "real" size of a datum, given the datum value,
  44.  * its type, whether it is a "by value", and its length.
  45.  *
  46.  * To cut a long story short, usually the real size is equal to the
  47.  * type length, with the exception of variable length types which have
  48.  * a length equal to -1. In this case, we have to look at the value of
  49.  * the datum itself (which is a pointer to a 'varlena' struct) to find
  50.  * its size.
  51.  *-------------------------------------------------------------------------
  52.  */
  53. Size
  54. datumGetSize(value, type, byVal, len)
  55. Datum value;
  56. ObjectId type;
  57. bool byVal;
  58. Size len;
  59. {
  60.  
  61.     struct varlena *s;
  62.     Size size;
  63.  
  64.     if (byVal) {
  65.     if (len >= 0 && len <= sizeof(Datum)) {
  66.         size = len;
  67.     } else {
  68.         elog(WARN,
  69.         "datumGetSize: Error: type=%ld, byVaL with len=%d",
  70.         (long) type, len);
  71.     }
  72.     } else { /*  not byValue */
  73.     if (len == -1) {
  74.         /*
  75.          * variable length type
  76.          * Look at the varlena struct for its real length...
  77.          */
  78.         s = (struct varlena *) DatumGetPointer(value);
  79.         if (!PointerIsValid(s)) {
  80.         elog(WARN,
  81.             "datumGetSize: Invalid Datum Pointer");
  82.         }
  83.         size = (Size) VARSIZE(s);
  84.     } else {
  85.         /*
  86.          * fixed length type
  87.          */
  88.         size = len;
  89.     }
  90.     }
  91.  
  92.     return(size);
  93. }
  94.  
  95. /*-------------------------------------------------------------------------
  96.  * datumCopy
  97.  *
  98.  * make a copy of a datum
  99.  *
  100.  * If the type of the datum is not passed by value (i.e. "byVal=false")
  101.  * then we assume that the datum contains a pointer and we copy all the
  102.  * bytes pointed by this pointer
  103.  *-------------------------------------------------------------------------
  104.  */
  105. Datum
  106. datumCopy(value, type, byVal, len)
  107. Datum value;
  108. ObjectId type;
  109. bool byVal;
  110. Size len;
  111. {
  112.  
  113.     Size realSize;
  114.     Datum res;
  115.     Pointer s;
  116.  
  117.     realSize = datumGetSize(value, type, byVal, len);
  118.  
  119.     if (byVal) {
  120.     res = value;
  121.     } else {
  122.     /*
  123.      * the value is a pointer. Allocate enough space
  124.      * and copy the pointed data.
  125.      */
  126.     s = (Pointer) palloc(realSize);
  127.     if (s == NULL) {
  128.         elog(WARN,"datumCopy: out of memory\n");
  129.     }
  130.     bcopy(DatumGetPointer(value), s, realSize);
  131.     res = PointerGetDatum(s);
  132.     }
  133.     return(res);
  134. }
  135.  
  136. /*-------------------------------------------------------------------------
  137.  * datumFree
  138.  *
  139.  * Free the space occupied by a datum CREATED BY "datumCopy"
  140.  *
  141.  * NOTE: DO NOT USE THIS ROUTINE with datums returned by amgetattr() etc.
  142.  * ONLY datums created by "datumCopy" can be freed!
  143.  *-------------------------------------------------------------------------
  144.  */
  145. void
  146. datumFree(value, type, byVal, len)
  147. Datum value;
  148. ObjectId type;
  149. bool byVal;
  150. Size len;
  151. {
  152.  
  153.     Size realSize;
  154.     Pointer s;
  155.  
  156.     realSize = datumGetSize(value, type, byVal, len);
  157.  
  158.     if (!byVal) {
  159.     /*
  160.      * free the space palloced by "datumCopy()"
  161.      */
  162.     s = DatumGetPointer(value);
  163.     pfree(s);
  164.     }
  165. }
  166.  
  167. /*-------------------------------------------------------------------------
  168.  * datumIsEqual
  169.  *
  170.  * Return true if two datums are equal, false otherwise
  171.  *
  172.  * NOTE: XXX!
  173.  * We just compare the bytes of the two values, one by one.
  174.  * This routine will return false if there are 2 different
  175.  * representations of the same value (something along the lines
  176.  * of say the representation of zero in one's complement arithmetic).
  177.  *
  178.  *-------------------------------------------------------------------------
  179.  */
  180. bool
  181. datumIsEqual(value1, value2, type, byVal, len)
  182. Datum value1;
  183. Datum value2;
  184. ObjectId type;
  185. bool byVal;
  186. Size len;
  187. {
  188.     Size size1, size2;
  189.     char *s1, *s2;
  190.  
  191.     if (byVal) {
  192.     /*
  193.      * just compare the two datums.
  194.      * NOTE: just comparing "len" bytes will not do the
  195.      * work, because we do not know how these bytes
  196.      * are aligned inside the "Datum".
  197.      */
  198.     if (value1 == value2)
  199.         return(true);
  200.     else
  201.         return(false);
  202.     } else {
  203.     /*
  204.      * byVal = false
  205.      * Compare the bytes pointed by the pointers stored in the
  206.      * datums.
  207.      */
  208.     size1 = datumGetSize(value1, type, byVal, len);
  209.     size2 = datumGetSize(value2, type, byVal, len);
  210.     if (size1 != size2)
  211.         return(false);
  212.     s1 = (char *) DatumGetPointer(value1);
  213.     s2 = (char *) DatumGetPointer(value2);
  214.     if (!bcmp(s1, s2, size1))
  215.         return(true);
  216.     else
  217.         return(false);
  218.     }
  219. }
  220.     
  221.